package com.banciyuan.bcywebview.biz.detail;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.utils.common.UIUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by hy on 2015/8/17.
 */
public class SearchPersonAdapter extends BaseAdapter {

    private static final int HEADERTYPE = 0;
    private static final int COMMONTYPE = 1;
    private List<String> names = new ArrayList<String>();
    private Context context;

    public SearchPersonAdapter(Context context, List<String> names) {
        this.context = context;
        this.names = names;
    }

    @Override
    public int getCount() {
        if (names.size() == 0)
            return 0;
        return names.size() + 1;
    }

    @Override
    public Object getItem(int i) {
        return names.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        int type = getItemViewType(position);
        if (type == COMMONTYPE) {
            if (null == convertView) {
                convertView = View.inflate(context, R.layout.search_person_item, null);
            }
            ((TextView) convertView).setText(names.get(position - 1));
            AbsListView.LayoutParams params = new AbsListView.LayoutParams(-1, UIUtils.dip2px(44, context));
            convertView.setLayoutParams(params);
        } else {
            convertView = new TextView(context);
            ((TextView) convertView).setText(context.getString(R.string.local_search_result));
            ((TextView) convertView).setTextColor(context.getResources().getColor(R.color.grey_ten_level));
            convertView.setPadding(UIUtils.dip2px(16, context), 0, 0, 0);
            ((TextView) convertView).setGravity(Gravity.CENTER_VERTICAL);
            AbsListView.LayoutParams params = new AbsListView.LayoutParams(-1, UIUtils.dip2px(26, context));
            convertView.setLayoutParams(params);
            convertView.setBackgroundColor(context.getResources().getColor(R.color.background));
        }
        return convertView;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return HEADERTYPE;
        } else {
            return COMMONTYPE;
        }
    }


}
